package cn.com.dashihui.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.com.dashihui.kit.ExcelExportUtil;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.kit.ListKit;
import cn.com.dashihui.web.dao.AdminSeller;
import cn.com.dashihui.web.dao.Category;
import cn.com.dashihui.web.dao.DetailsImg;
import cn.com.dashihui.web.dao.GoodsBaseImages;
import cn.com.dashihui.web.dao.GoodsItem;
import cn.com.dashihui.web.dao.GoodsSale;
import cn.com.dashihui.web.dao.GoodsSaleGT;
import cn.com.dashihui.web.dao.GoodsSaleItem;
import cn.com.dashihui.web.dao.GoodsSaleOption;
import cn.com.dashihui.web.dao.GoodsSaleSpec;
import cn.com.dashihui.web.dao.GoodsSaleTemplate;
import cn.com.dashihui.web.dao.GoodsStore;
import cn.com.dashihui.web.dao.GoodsStoreRel;
import cn.com.dashihui.web.dao.GoodsToker;
import cn.com.dashihui.web.dao.Seller;
public class GoodsBaseService {
	/**
	 * 分页查找商品
	 * @param pageNum
	 * @param pageSize
	 */
	public Page<Record> findByPage(int pageNum,int pageSize,int sGoodId,String sGoodName,int sGb,String sSellerId,String sStoreId,int state,int sCategoryonid,int sCategorytwid,int sCategorythid,int sCategoryfoid){
		StringBuffer sBuffer = new StringBuffer(""
				+ "FROM t_bus_goods AS A "
				+ "inner join t_bus_goods_store_rel F on A.good_store=F.goodstore "
				+ "inner JOIN t_dict_store AS C ON F.storeid= C.storeid "
				+ "inner JOIN t_bus_seller AS B ON B.sellerId= C.sellerId "
				+ "LEFT JOIN t_dict_category D on A.categoryonid=D.categoryId "
				+ "LEFT JOIN t_dict_category E on A.categorytwid=E.categoryId "
				+ "LEFT JOIN t_dict_category G on A.categorythid=G.categoryId "
				+ "LEFT JOIN t_dict_category H on A.categoryfoid=H.categoryId "
				+ "WHERE 1=1");
		List<Object> params = new ArrayList<Object>();
		sBuffer.append(" AND B.sellerId= ?");
		params.add(sSellerId);
		/*if(!StrKit.isBlank(sSellerId)&&!"0".equals(sSellerId)){
		sBuffer.append(" AND B.sellerId= ?");
		params.add(sSellerId);
		}*/
		if(sGoodId!=0){
			sBuffer.append(" AND A.id=?");
			params.add(sGoodId);
		}
		if(StrKit.notBlank(sGoodName)){
			sBuffer.append(" AND A.name like ?");
			params.add("%"+sGoodName+"%");
		}
		if(sGb!=0){
			sBuffer.append(" AND A.groupbuying=?");
			params.add(sGb);
		}
		if(!StrKit.isBlank(sStoreId)&&!"0".equals(sStoreId)){
			sBuffer.append(" AND C.storeid in ('"+sStoreId+"')");
		}
		/*if(!StrKit.isBlank(sStoreId)&&!"0".equals(sStoreId)){
			sBuffer.append(" AND C.storeId= ?");
			params.add(sStoreId);
		}*/
		 if(sCategoryfoid!=0){
			sBuffer.append(" AND A.categoryonid=? AND A.categorytwid=? AND A.categorythid=? AND A.categoryfoid=?");
			params.add(sCategoryonid);params.add(sCategorytwid);params.add(sCategorythid);params.add(sCategoryfoid);
		}else if(sCategorythid!=0){
			sBuffer.append(" AND A.categoryonid=? AND A.categorytwid=? AND A.categorythid=?");
			params.add(sCategoryonid);params.add(sCategorytwid);params.add(sCategorythid);
		}else if(sCategorytwid!=0){
			sBuffer.append(" AND A.categoryonid=? AND A.categorytwid=?");
			params.add(sCategoryonid);params.add(sCategorytwid);
		}else if(sCategoryonid!=0){
			sBuffer.append(" AND A.categoryonid=?");
			params.add(sCategoryonid);
		}
		switch (state) {
			case 1:
				sBuffer.append(" AND A.pstate=1 and A.state=1");
				break;
			case 2:
				sBuffer.append(" AND A.pstate=1 and A.state=2");
				break;
			case 3:
				sBuffer.append(" AND A.pstate=1 and A.state=3");
				break;
			case 4:
				sBuffer.append(" AND A.pstate=2");
				break;
			case 5:
				sBuffer.append(" AND A.pstate=3");
				break;
		}
		/*if(!StrKit.isBlank(keyword)){
			sBuffer.append(" AND A.name LIKE ?");
			params.add("%"+keyword+"%");
		}*/
		sBuffer.append(" group by A.id");
		sBuffer.append(" ORDER BY A.createDate DESC");
		 
		//return Db.paginate(pageNum, pageSize, "SELECT A.*, C.title,D.categoryName as oneName,E.categoryName as twoName", sBuffer.toString(), params.toArray());
		Page<Record> page=	Db.paginate(pageNum, pageSize, "SELECT A.*, ifnull((SELECT state from t_bus_goods_toker AS TK WHERE A.id=TK.goodsId group by id),0) as isToker , GROUP_CONCAT(C.title) as title,D.categoryName as oneName,E.categoryName as twoName", sBuffer.toString(), params.toArray());
			
		if(page.getList()!=null&&page.getList().size()!=0){
			//将各订单对应的订单商品查出
			return new Page<Record>(findGoodsByStorid(page.getList()), page.getPageNumber(), page.getPageSize(), page.getTotalPage(), page.getTotalRow());
		}
	   
		return  page;
	}
	
	/**
	 * 一个商品对应多个分公司，显示时以商品为主。
	 * 
	 */
	private List<Record> findGoodsByStorid(List<Record> StoreList){
		//拼接SQL
	StringBuffer sBuffer = new StringBuffer("SELECT C.title, A.* FROM t_bus_goods AS A inner join t_bus_goods_store_rel F on A.good_store=F.goodstore "
			+ "inner JOIN t_dict_store AS C ON F.storeid= C.storeid ");
	sBuffer.append("WHERE A.good_store IN (");
	for(Record Store : StoreList){
		sBuffer.append("'").append(Store.getStr("good_store")).append("',");
	}
	sBuffer.replace(sBuffer.lastIndexOf(","), sBuffer.length(), ")");
	sBuffer.append(" ORDER BY A.good_store");
	    //整理结果
			List<Record> goodsList = Db.find(sBuffer.toString());
			if(goodsList!=null){
				//先将查出来的所有的订单商品，按订单号归类
				Map<String,List<Record>> goodsMap = new HashMap<String,List<Record>>();
				for(Record goods : goodsList){
					if(goodsMap.containsKey(goods.getStr("good_store"))){
						goodsMap.get(goods.getStr("good_store")).add(goods);
					}else{
						goodsMap.put(goods.getStr("good_store"), new ListKit<Record>().add(goods).getList());
					}
				}
				//遍历订单列表的同时，设置对应订单商品集
				for(Record order : StoreList){
					order.set("goodsList", goodsMap.get(order.get("good_store")));
				}
			}
			return StoreList;
	
	}
	/**
	 * 查找商品团购属性
	 */
	public GoodsStore findGbById(int id){
		return GoodsStore.me().findFirst("SELECT groupbuying t_bus_goods from WHERE A.id=?",id);
	}
	/**
	 * 查找商品
	 */
	public GoodsStore findById(int id){
		return GoodsStore.me().findFirst("SELECT A.*, C.title,D.categoryName as oneName,E.categoryName as twoName "
				+ "FROM t_bus_goods AS A inner join t_bus_goods_store_rel F on A.good_store=F.goodstore inner JOIN t_dict_store AS C ON F.storeid= C.storeid inner JOIN t_bus_seller AS B ON B.sellerId= C.sellerId "
				+ "LEFT JOIN t_dict_category D on A.categoryonid=D.categoryId LEFT JOIN t_dict_category E on A.categorytwid=E.categoryId WHERE A.id=?",id);
	}
	public GoodsStore findByIdGoods(int id){
		return GoodsStore.me().findFirst("SELECT * FROM  t_bus_goods WHERE id=?",id);
	}
	/**
	 * 根据商品名字获取商品ID  按时间排序倒序第一个
	 * @param name
	 * @return
	 */
	public int findidByName(String name){
		return GoodsStore.me().findFirst("SELECT * FROM  t_bus_goods WHERE name=? order by createDate desc",name).get("id");
	}
	/**
	 * 增加商品
	 */
	public boolean add(GoodsStore goods){
		return goods.save();
	}
	/**
	 *修改商品
	 */
	public boolean updategood(GoodsStore goods){
		return goods.update();
	}
	/**
	 * 轮播图片排序
	 * @param sortMap
	 * @return
	 */
	public boolean sortImages(Map<String,String> sortMap){
		int batchSize = sortMap.size();
		List<String> sqlList = new ArrayList<String>();
		for(String id : sortMap.keySet()){
			sqlList.add("UPDATE t_bus_goods_base_images SET orderNo="+sortMap.get(id)+" WHERE id="+id);
		}
		int[] result = Db.batch(sqlList,batchSize);
		return result.length>0;
	}
	
	/**
	 * 详情图片排序
	 * @param sortMap
	 * @return
	 */
	public boolean sortDetailImages(Map<String,String> sortMap){
		int batchSize = sortMap.size();
		List<String> sqlList = new ArrayList<String>();
		for(String id : sortMap.keySet()){
			sqlList.add("UPDATE goods_details_images SET orderNo="+sortMap.get(id)+" WHERE id="+id);
		}
		int[] result = Db.batch(sqlList,batchSize);
		return result.length>0;
	}
	
	/**
	 * 商品图片列表
	 * 
	 * @return
	 */
	public List<GoodsBaseImages> findAllImages(String LunBoNum){
		return GoodsBaseImages.me().find("SELECT A.* FROM t_bus_goods_base_images A WHERE A.LunBoNum=? ORDER BY A.orderNo",LunBoNum);
	}
	
	/**
	 * 商品详情图片列表
	 * 
	 * @return
	 */
	public List<DetailsImg> findDetailImages(String LunBoNum){
		return DetailsImg.me().find("SELECT A.* FROM goods_details_images A WHERE A.LunBoNum=? ORDER BY A.orderNo",LunBoNum);
	}
	
	
	/**
	 * 添加图片
	 * @param newObject
	 * @return
	 */
	public boolean addImage(GoodsBaseImages newObject){
		return newObject.save();
	}
	
	
	/**
	 * 添加详情图片
	 * @param newObject
	 * @return
	 */
	public boolean addDetailImage(DetailsImg newObject){
		return newObject.save();
	}
	
	
	/**
	 * 删除图片
	 * @param id
	 * @return
	 */
	public boolean delImage(int id){
		return GoodsBaseImages.me().deleteById(id);
	}
	
	
	/**
	 * 删除详情图片
	 * @param id
	 * @return
	 */
	public boolean delDetailImage(int id){
		return DetailsImg.me().deleteById(id);
	}
	
	
	/**
	 * 设置图片为商品logo
	 * @param id
	 * @return
	 */
	@Before(Tx.class)
	public boolean setImageLogo(int goodsid,int id){
		List<String> sqlList = new ArrayList<String>();
		//更新该商品所有LOGO的isLogo标识为0
		sqlList.add("UPDATE t_bus_goods_base_images SET isLogo=0 WHERE goodsid="+goodsid);
		//更新当前LOGO的isLogo标识为1
		sqlList.add("UPDATE t_bus_goods_base_images SET isLogo=1 WHERE goodsid="+goodsid+" AND id="+id);
		//更新该商品的LOGO字段值为当前LOGO的地址
		sqlList.add("UPDATE t_bus_goods_base SET thumb=(SELECT thumb FROM t_bus_goods_base_images WHERE id="+id+") WHERE id="+goodsid);
		Db.batch(sqlList, sqlList.size());
		return true;
	}
	public int doCancel(String id, String checktime) {
		return Db.update("update t_bus_goods set state=3 ,checktime=? WHERE id=?",checktime,id);
	}
	public int doAccept(String id, String checktime) {
		return Db.update("update t_bus_goods set state=2 ,checktime=? WHERE id=?",checktime,id);
	}
	public boolean delete(GoodsStore goods) {
		return goods.delete();
	}
	public int doDown(String id, String downtime) {
		return Db.update("update t_bus_goods set state=3 ,pstate=1,downtime=? WHERE id=?",downtime,id);
	}
	public int doUp( String id,String createDate ) {
		return Db.update("update t_bus_goods set state=1 ,pstate=1 ,createDate=? WHERE id=?",createDate,id);
	}
	/**
	 * @param id
	 * @return
	 */
	public int doclerk( String id ) {
		return Db.update("update t_bus_goods set pstate=2 ,stock=0 WHERE id=?",id);
	}
	public int tooDown(String id, String checktime) {
		return Db.update("update t_bus_goods set state=5 ,checktime=? WHERE id=?",checktime,id);
	}
	public Record findGoodDetailById(int id) {
			return Db.findFirst("select * FROM t_bus_goods WHERE id=?",id);
	}
	public List<Record> getGoodsItem(String gooditem) {
		return Db.find("select * FROM t_bus_goods_item WHERE goodItem=?",gooditem);
	}
	
	/**
	 * 删除规格
	 * @param goodId
	 * @return
	 */
	public int deteleSpec(int goodId) {
		return Db.update("DELETE FROM t_bus_goods_spec WHERE goodId=?",goodId);
	}
	/**
	 * 
	 * @param gooditem
	 * @return
	 */
	public List<Record> getGoodSpec(int goodsId) {
		return Db.find("select * FROM t_bus_goods_spec WHERE goodId=?",goodsId);
	}
	public int getGoodsItemid(int id) {
		return Db.queryInt("select B.id FROM t_bus_goods AS A  inner JOIN  t_bus_goods_item AS B ON A.goodItem=B.goodItem WHERE A.id=？",id);
	}
	public int deteleItem(String goodItem) {
		return Db.update("DELETE FROM t_bus_goods_item WHERE goodItem=?",goodItem);
	}
	public int detelepayType(int id) {
		return Db.update("update t_bus_goods set sellpoint=0,sellPrice=0  WHERE id=?",id);
	}
	public int detelejyrs(int id) {
		return Db.update("update t_bus_goods set jyrs=0 WHERE id=?",id);
	}
	public int detelezdrs(int id) {
		return Db.update("update t_bus_goods set zdrs=0 WHERE id=?",id);
	}
	public int detelestore(String goodstore) {
		return Db.update("DELETE FROM t_bus_goods_store_rel WHERE goodstore=?",goodstore);
	}
	
	/**
	 * 删除商品的规格
	 * @param goodId
	 * @return
	 */
	public int deteleGoodSpec(int goodId) {
		return Db.update("DELETE FROM t_bus_goods_spec WHERE goodId=?",goodId);
	}
	/*public String getgoodstore(String storeid) {
		return Db.queryStr("select goodstore FROM t_bus_goods_store_rel  WHERE storeid=?",storeid);
	}
	public int getgoodstoreid(String storeid) {
		return Db.queryInt("select id FROM t_bus_goods_store_rel  WHERE storeid=?",storeid);
	}*/
	public List<Record> getStore(String good_store) {
		return Db.find("SELECT A.title,A.address,A.tel,A.title FROM t_dict_store A WHERE A.storeid IN (select storeid FROM t_bus_goods_store_rel WHERE goodstore=?) and A.qstate=1 and A.state=2 ",good_store);
	}
	public Seller getSellerId(int userid) {
		return Seller.me().findFirst("select sellerId FROM t_bus_seller WHERE userid=?",userid);
	}
	public void addItem(GoodsItem goodsitem) {
		goodsitem.save();
	}
	
	public void detelegoodstore(GoodsStoreRel goodstore) {
		goodstore.delete();
	}
	public void updateItem(GoodsItem goodsitem) {
		goodsitem.update();
	}
	
	
	
	/**
	 * 查询公司的状态是否通过
	 * @return
	 */
	public Record findState(int userid,int RoleId) {
		// TODO Auto-generated method stub
		if(RoleId==1||RoleId==2){
			return Db.findFirst("SELECT  * FROM  t_bus_seller WHERE userid=? ",userid);
		}else{
			return Db.findFirst("SELECT  * FROM  t_dict_store WHERE storeid=(select storeid FROM t_auth_admin_seller WHERE id=?)",userid);
		}
	}
	
	public String findselleid(int userid){
		
		return Db.queryStr("SELECT  sellerId FROM  t_bus_seller WHERE userid=? ",userid);
	}
	
	/**
	 * 查询分公司的开店状态是否正常
	 * @return
	 */
	public Record findQstate(int userid) {
		return Db.findFirst("SELECT  qstate FROM  t_dict_store WHERE storeid=(select storeid FROM t_auth_admin_seller WHERE id=?)",userid);
	}
	/**
	 * 从对象中获取公司sellerid
	 */
	public Record FindById(int userId) {
		return Db.findFirst("select * FROM t_bus_seller WHERE userId=?",userId);
	}
	public Record findByUserId(int userid){
		return Db.findFirst("select sellerId FROM t_bus_seller WHERE userid=?",userid);
	}
	/**
	 * 分公司Id查所属公司
	 * @param storeid
	 * @return
	 */
	public Record findBysellerId(String storeid){
		return Db.findFirst("select sellerId FROM t_dict_store WHERE storeid = ?",storeid);
	}
	
	/**
	 * 根据userid,获取角色
	 * @param userid
	 * @return
	 */
	public int findByRoleId(int userid){
		return Db.queryInt("SELECT C.id  FROM t_auth_admin_seller as A inner JOIN  t_auth_admin_roles_seller as B ON A.id=B.userid  INNER JOIN t_auth_roles_seller AS C ON B.roleid = C.id WHERE userid=?" ,userid);
	}
	/**
	 * 查询分公司的状态是否通过
	 * @return
	 */
	public Record findStoreState(int userid) {
		// TODO Auto-generated method stub
		return Db.findFirst("SELECT  A.storeid FROM  t_dict_store as A inner JOIN t_auth_admin_seller as B ON A.storeid=B.storeid WHERE B.id=?",userid);
		
	}
	/**
	 * 导出数据
	 * @param response
	 * @param request
	 * @param listz 
	 * @param titles2 
	 * @param member
	 */
	public void export(HttpServletResponse response, HttpServletRequest request, List<Pair> titles, List<Record> listz) {
		ExcelExportUtil.exportByRecord(response, request, "商品上架列表", titles, listz);
	}
	public List<Record> findExcelByPage(int sGoodId, String sSellerId, String sGoodName, String sStoreId, int state, int one, int two) {
		StringBuffer sBuffer = new StringBuffer("SELECT A.*, GROUP_CONCAT(C.title) as title,D.categoryName as oneName,E.categoryName as twoName,G.state as isToker FROM t_bus_goods AS A inner join t_bus_goods_store_rel F on A.good_store=F.goodstore inner JOIN t_dict_store AS C ON F.storeid= C.storeid inner JOIN t_bus_seller AS B ON B.sellerId= C.sellerId "
				+ "LEFT JOIN t_dict_category D on A.categoryonid=D.categoryId LEFT JOIN t_dict_category E on A.categorytwid=E.categoryId LEFT JOIN t_bus_goods_toker G on A.id=G.goodsId  WHERE 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StrKit.isBlank(sSellerId)&&!"0".equals(sSellerId)){
		sBuffer.append(" AND B.sellerId= ?");
		params.add(sSellerId);
		}
		if(sGoodId!=0){
			sBuffer.append(" AND A.id=?");
			params.add(sGoodId);
		}
		if (StrKit.notBlank(sGoodName)) {
			sBuffer.append(" AND A.name like ?");
			params.add("%"+sGoodName+"%");
		}
		if(!StrKit.isBlank(sStoreId)&&!"0".equals(sStoreId)){
			sBuffer.append(" AND C.storeid in ('"+sStoreId+"')");
		}
	/*	if(!StrKit.isBlank(sStoreId)&&!"0".equals(sStoreId)){
			sBuffer.append(" AND C.storeId= ?");
			params.add(sStoreId);
		}*/
	    if(two!=0){
			sBuffer.append(" AND A.categoryonid=? AND A.categorytwid=?");
			params.add(one);params.add(two);
		}else if(one!=0){
			sBuffer.append(" AND A.categoryonid=?");
			params.add(one);
		}
		switch (state) {
			case 1:
				sBuffer.append(" AND A.pstate=1 and A.state=1");
				break;
			case 2:
				sBuffer.append(" AND A.pstate=1 and A.state=2");
				break;
			case 3:
				sBuffer.append(" AND A.pstate=1 and A.state=3");
				break;
			case 4:
				sBuffer.append(" AND A.pstate=2");
				break;
			case 5:
				sBuffer.append(" AND A.pstate=3");
				break;
		}
		sBuffer.append(" group by A.id");
		sBuffer.append(" ORDER BY A.createDate DESC");
		return Db.find(sBuffer.toString(), params.toArray());
	}
	/**
	 * 查询所有的模板信息
	 */
	public List<GoodsSaleTemplate> findAllGoodsTemplates(){
		return GoodsSaleTemplate.me().find("SELECT * FROM t_bus_goods_sale_template");
	}
	/**
	 * 存储商品模板关联信息
	 */
	public boolean savegt(GoodsSaleGT goods){
		return goods.save();
	}
	/**
	 * 查询当前商品是否已关联模板
	 */
	public GoodsSaleGT findTemBygoodsid(int goodsid){
		return GoodsSaleGT.me().findFirst("SELECT * FROM t_bus_goods_sale_gt WHERE goodsid=?" ,goodsid);
	}
	/**
	 * 根据模板id查询模板信息
	 * @param tid
	 * @return
	 */
	public GoodsSaleTemplate findTemplateById(int tid) {
		// TODO Auto-generated method stub
		return GoodsSaleTemplate.me().findFirst("SELECT * FROM t_bus_goods_sale_template WHERE id = ?",tid);
	}
	/**
	 * 根据规格id查询对应option
	 * @param specid
	 * @param id 
	 * @return 
	 */
	/*public  List<Option> finOptionById(int specid, int id) {
		// TODO Auto-generated method stub
		return Option.me().find("SELECT id FROM t_bus_goods_sale_option WHERE specid = ?",specid).get(id);
	}*/
	public List<GoodsSaleOption> finOptionById(Integer specid){
		return GoodsSaleOption.me().find("SELECT * FROM  t_bus_goods_sale_option o WHERE specid=?",specid);
	}
	/**
	 * 根据id查询规格名称
	 * @param iid
	 * @return
	 */
	public GoodsSaleSpec findSpecByid(int specid) {
		// TODO Auto-generated method stub
		return GoodsSaleSpec.me().findFirst("SELECT * FROM t_bus_goods_sale_spec WHERE id = ?",specid);
	}
	/**
	 * 根据id查询参数名称
	 * @param id
	 * @return
	 */
	public String findOptionNameByid(int id){
		return Category.me().findFirst("SELECT o.optionName FROM  t_bus_goods_sale_option o WHERE id=?",id).get("optionName");
	}	
	/**
	 * 寻找拓客表中商品id为goodsid的信息
	 * @param goodsid
	 * @return
	 */
	public GoodsToker findTokerById(int goodsid){
		return GoodsToker.me().findFirst("SELECT * FROM t_bus_goods_toker  WHERE goodsid=?",goodsid);
	}

	
	/**
	 * 
	 * 自定义参数存储
	 * @param option
	 * @return
	 */
	public boolean saveItem(GoodsSaleItem goodsSaleItem){
		return goodsSaleItem.save();
	}
	/**
	 * 删除当前商品对应的扩展信息
	 * @param goodId
	 * @return
	 */
	public int deleteitem(int goodsid) {
		return Db.update("DELETE FROM t_bus_goods_sale_items WHERE goodsid=?",goodsid);
	}
	/**
	 * 根据商品id查询所有的SKU
	 * @param id
	 * @return
	 */
	public List<GoodsSale> findAllSale(int goodsid){
		return GoodsSale.me().find("SELECT * FROM  t_bus_goods_sale  WHERE goodsid=?",goodsid);
	}
	/**
	 * 根据商品id查询所有的扩展信息
	 * @param iid
	 * @return
	 */
	public List<GoodsItem> findAllItems(int goodsid){
		return GoodsItem.me().find("SELECT * FROM  t_bus_goods_sale_items  WHERE goodsid=?",goodsid);
	}
	/**
	 * 根据id删除对应的Sku
	 * @param goodId
	 * @return
	 */
	public int deteleSale(int goodsid) {
		return Db.update("DELETE FROM t_bus_goods_sale WHERE goodsid=?",goodsid);
	}

	/**
	 * 查询所有的一级category
	 * @param iid
	 * @return
	 */
	public List<Category> finOneCategory(){
		return Category.me().find("SELECT * FROM  t_dict_category  WHERE categoryType=1");
	}
	/**
	 * 查询对应的二级category
	 * @param iid
	 * @return
	 */
	public List<Record> finTwoCategory(String categoryonid) {
		return Db.find("select categoryId,categoryName from t_dict_category where categoryFatherId in ('"+categoryonid+"')");
	}
	/**
	 * 查询对应的三级category
	 * @param iid
	 * @return
	 */
	public List<Record> finThreeCategory(String categorytwid) {
		return Db.find("select categoryId,categoryName from t_dict_category where categoryFatherId in ('"+categorytwid+"')");
	}
	/**
	 * 查询对应的四级category
	 * @param iid
	 * @return
	 */
	public List<Record> finFourCategory(String categorythid) {
		return Db.find("select categoryId,categoryName from t_dict_category where categoryFatherId in ('"+categorythid+"')");
	}
	/**
	 * 查询所有的二级category
	 * @param iid
	 * @return
	 */
	public List<Category> findTwoCategory(){
		return Category.me().find("SELECT * FROM  t_dict_category  WHERE categoryType=2");
	}
	/**
	 * 查询所有的三级category
	 * @param iid
	 * @return
	 */
	public List<Category> findThreeCategory(){
		return Category.me().find("SELECT * FROM  t_dict_category  WHERE categoryType=3");
	}
	/**
	 * 查询所有的四级category
	 * @param iid
	 * @return
	 */
	public List<Category> findFourCategory(){
		return Category.me().find("SELECT * FROM  t_dict_category  WHERE categoryType=4");
	}
	/**
	 * 查询所有的扩展信息
	 * @return
	 */
	public List<GoodsSaleItem> findSaleItem(int goodsid){
		return GoodsSaleItem.me().find("SELECT * FROM  t_bus_goods_sale_items where goodsid =?",goodsid);
	}

	/**
	 * 根据id查找adseller
	 * @param goodsid
	 * @return
	 */
	public AdminSeller findAdseller(String username){
		return AdminSeller.me().findFirst("SELECT * FROM t_auth_admin_seller  WHERE username=?",username);
	}
	/**
	 * 查询当前用户
	 * @param userID、
	 * 
	 * @return
	 */
	public AdminSeller findUserById(int userid){
		return AdminSeller.me().findFirst("SELECT * FROM t_auth_admin_seller WHERE id=?",userid);
	}
}